<?php

//session
session_start();
if(isset($_SESSION['usr_name'])) {
	header("Location: index.php");
}

include_once '../sql/dbconnect.php';

//set validation error flag as false
$error = false;

//check if form is submitted
if (isset($_POST['register'])) {
	$phone = mysqli_real_escape_string($con, $_POST['phone']);
	$password = mysqli_real_escape_string($con, $_POST['password']);
	$cpassword = mysqli_real_escape_string($con, $_POST['cpassword']);
	
	//name can contain only alpha characters and space
	if(!preg_match("/^1[34578]{1}\d{9}$/",$phone)){
		$error = true;
	}
	if(strlen($password) < 6) {
		$error = true;
		$password_error = "Password must be minimum of 6 characters";
	}
	if($password != $cpassword) {
		$error = true;
		$cpassword_error = "Password and Confirm Password doesn't match";
	}
	if (!$error) {
		if(mysqli_query($con, "INSERT INTO userinfo(phone,password) VALUES('" . $phone . "', '" . md5($password) . "')")) {
			$successmsg = "恭喜注册成功！点击登录进行登录~";
		} else {
			$errormsg = "对不起，该手机号已经被注册！";
		}
	}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>注册页面</title>
	<link rel="stylesheet" type="text/css" href="../css/index.css">
	<link rel="stylesheet" type="text/css" href="../css/common.css">
	<link rel="shortcut icon" href="http://ucenter.b5m.com/favicon.ico?_=" type="image/x-ion">
	<script type="text/javascript" src="../js/jQuery.js"></script>
	<script type="text/javascript" src="../js/template.js"></script>
	<script type="text/javascript" src="../js/index.js"></script>
	<script type="text/javascript" src="../js/cookie.js"></script>
</head>
<body>
	<div id="main_login" class="common overHidden minWidth">
		<div class="logo margin_auto"><a href="index.php"><img src="../img/logo1.png"></a></div>
		<h2 class="margin_auto">MyWay，畅享全球品质生活！</h2>
		<div class="toast-msg margin_auto"><?php if (isset($successmsg)) { echo $successmsg; }; if (isset($errormsg)) { echo $errormsg; };?></div>
		<p class="margin_auto">欢迎注册</p>
		<div class="form_check margin_auto">
			<form  action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" onsubmit="return disabledSub();">
				<ul>
					<li class="txtLi">
						<input type="text" class="txt" placeholder="验证手机" name="phone">
						<p class="msg">完成验证后，您可以用该手机号登录和找回密码</p>

					</li>
					<li class="vcLi">
						<input type="text" class="vc_text vc1" name="" placeholder="图片验证码">
						<p class="msg">输入右边图片文字</p>
						<p class="vc_code right">
							<b class="code_show"></b>
							<span class="code_reset"><a href="javascript:;">点击<br>换一换</a></span>
						</p>
						
						<!-- <p class="vc_war">验证码错误！</p> -->
					</li>
					<li class="psdLi">
						<input type="password" class="psd1" placeholder="请设置密码" name="password">
						<p class="msg">6-15位字母，建议字母、数字及下划线两种以上组合</p>
					</li>
					<li class="psdLi">
						<input type="password" class="psd2" placeholder="请确认密码" name="cpassword">
						<p class="msg">请再次输入密码</p>
					</li>
					<li class="access">
						<input type="checkbox" class="cb" name="">我已阅读并同意《帮5买用户注册协议》
					</li>
					<li class="login">
						<input class="submit register-submit" type="submit" name="register" value="注　册">
					</li>
					<li class="reg">
						<a href="userenter.php">登录</a>
					</li>
				</ul>
			</form>
		</div>
	</div>
	<script type="text/javascript">
		var codeInp = $('.vc_text'),
			codeShow = $('.code_show');
		//验证码错误阻止提交
		function disabledSub(event){
			if(codeInp.val().toLowerCase() != codeShow.text().toLowerCase()){
				codeInp.next().addClass("error_msg").css({display:"block"});
	    		codeInp.next().html("图片验证码错误");
	    		setTimeout(removeErr,2000);
	    		function removeErr(){
	    			codeInp.next().removeClass("error_msg").html("输入右边图片数字进行验证").css({display:"none"});
		    	}
				$('.submit').focus();
				var event = event || window.event;
				event.preventDefault(); // 兼容标准浏览器
  				window.event.returnValue = false; // 兼容IE6~8
			}
		}
	</script>
</body>
</html>